<?php 
namespace app\common\lib;

/**
 * 日期类
 */
class Date
{
	private $timestamp;
	function __construct( $timestamp = null )
	{
		if( empty( $timestamp ) || !is_numeric( $timestamp ) ){
			$timestamp = time();
		}
		$this->setTimestamp( $timestamp );
	}
	/**
	 * 设置时间戳
	 */
	private function setTimestamp( $timestamp ){
		$this->timestamp = $timestamp;
	}
	/**
	 * 获取格式化时间
	 */
	public function getFormatDate( $format = '1' ){
		if( empty( $format ) ||  !is_string( $format ) ){
			$format = "1";
		}
		switch ( $format ) {
			case '1':
				$format = "Y-m-d H:i:s";
				break;
			case '2':
				$format = "Y-m-d";
				break;
			case '3':
				$format = "Y年m月d日 H时i分s秒";
				break;
			case '4':
				$format = "Y年m月d日";
				break;
			default:
				break;
		}
		return date( $format , $this->timestamp );
	}

	/**
	 * 获取年份
	 */
	public function getYear(){
		return (int)( date( "Y" , $this->timestamp ) );
	}

	/**
	 * 获取月份
	 */
	public function getMonth(){
		return (int)( date( "n" , $this->timestamp ) );
	}

	/**
	 * 获取日
	 */
	public function getDate(){
		return (int)( date( "d" , $this->timestamp ) );
	}

	/**
	 * 获取时
	 */
	public function getHour(){
		return (int)( date( "H" , $this->timestamp ) );
	}

	/**
	 * 获取分
	 */
	public function getMinute(){
		return (int)( date( "m" , $this->timestamp ) );
	}

	/**
	 * 获取秒 
	 */
	public function getSecond(){
		return (int)( date( "s" , $this->timestamp ) );
	}

	/**
	 * 月的天数
	 */
	public function getMonthDays()
	{
		return (int)( date( "t" , $this->timestamp ) );
	}

	/**
	 * 获取月中的第一天
	 */
	public function getMonthFirst( $isTimestamp = true )
	{
		$date = date( 'Y-m-01', $this->timestamp );
		if( !$isTimestamp ){
			return $date;
		}
		return strtotime( $date );
	}

	/**
	 * 获取月中的最后一天
	 */
	public function getMonthEnd( $isTimestamp = true )
	{
		$date = date( 'Y-m-'.$this->getMonthDays()." 23:59:59" , $this->timestamp );
		if( !$isTimestamp ){
			return $date;
		}
		return strtotime( $date );
	}

}